from typing import Optional
from pydantic import BaseModel, model_validator , ConfigDict


class UserCreate(BaseModel):
    account: str
    password: str

class User(BaseModel):
    id: int
    username: Optional[str]=None
    age: Optional[int] = None
    name: Optional[str] = None
    sex: Optional[str] = None
    phone: Optional[str] = None
    address: Optional[str] = None
    email: Optional[str] = None
    password: Optional[str] = None
    life: Optional[str] = None
    account: Optional[str] = None
    introduction: Optional[str] = None
    head_image: Optional[str] = None
    hash_ps: Optional[str] = None
    admin:str
    status:Optional[str] = None
    app_index_image:Optional[str]=None
    index_background: Optional[str] = None
    friends_count:Optional[int]=None
    groups_count:Optional[int]=None
    @model_validator(mode='before')
    def remove_unwanted_fields(cls, values):
        # print(values)
        # 过滤掉不允许的
        allowed_fields = {'groups_count','id','friends_count','account', 'app_index_image','username', 'age', 'sex', 'phone', 'email', 'head_image','admin','name',"introduction","index_background","address","status"}
        return {key: getattr(values, key) for key in allowed_fields if hasattr(values, key)}

class Token(BaseModel):
    access_token: str  # Token 字符串
    token_type: str  # Token 类型（一般为 "bearer"）
    user:User

class check_token_response(BaseModel):
    status:str
    user:User

class reset_user_info(BaseModel):
    id:Optional[int]=None
    account:Optional[str]=None
    username: Optional[str]=None
    age: Optional[int] = None
    name: Optional[str] = None
    sex: Optional[str] = None
    phone: Optional[str] = None
    address: Optional[str] = None
    email: Optional[str] = None
    password: Optional[str] = None
    introduction: Optional[str] = None
    head_image: Optional[str] = None
    admin: Optional[str] = None
    app_index_image: Optional[str] = None




class user_info_public(BaseModel):
    model_config = ConfigDict(from_attributes=True, populate_by_name=True)
    id:Optional[int]
    username:Optional[str]
    age: Optional[int]
    sex: Optional[str] = None
    email: Optional[str] = None
    introduction: Optional[str] = None
    head_image: Optional[str] = None
    # is_friend:Optional[bool]=None

    @model_validator(mode='before')
    def filter_fields(cls, data):
        allowed_fields = {
            "id","username","age","sex","email","introduction","head_image","admin","is_friend",
        }
        if isinstance(data, dict):
            # 如果输入是字典，直接过滤
            return {k: v for k, v in data.items() if k in allowed_fields}
        else:
            # 如果输入是SQLAlchemy模型实例
            return {k: getattr(data, k) for k in allowed_fields if hasattr(data, k)}

    def to_dict(self) -> dict:

        result = {}
        for field_name, field_value in self.model_dump().items():

            if field_value is not None:
                result[field_name] = field_value
        return result










